package cn.xiaomagege.platform.template.directive;

import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.util.Map;

public class BaseDirective {
    
    public String getString(String paramName, TemplateModel paramValue) throws TemplateModelException {
        if(paramValue == null) {
            return "";
        } else {
            if(paramValue instanceof TemplateScalarModel) { 
                String value = ((TemplateScalarModel) paramValue).getAsString();
                return value;
            } else {
                throw new TemplateModelException("String转换异常!");
            }
        }
    }
    
    public long getLong(String paramName, TemplateModel paramValue) throws TemplateModelException {
        if(paramValue == null) {
            return 0;
        } else {
            if(paramValue instanceof TemplateScalarModel) {
                String value = ((TemplateScalarModel) paramValue).getAsString();
                return Long.valueOf(value);
            } else if(paramValue instanceof TemplateNumberModel) {
                return ((TemplateNumberModel) paramValue).getAsNumber().longValue();
            } else {
                throw new TemplateModelException("Long转换异常!");
            }
        }
    }
    
    public int getInt(String paramName, TemplateModel paramValue) throws TemplateModelException {
        if(paramValue == null) {
            return 0;
        } else {
            if(paramValue instanceof TemplateScalarModel) {
                String value = ((TemplateScalarModel) paramValue).getAsString();
                return Integer.valueOf(value);
            } else if(paramValue instanceof TemplateNumberModel) {
                return ((TemplateNumberModel) paramValue).getAsNumber().intValue();
            } else {
                throw new TemplateModelException("int转换异常!");
            }
        }
    }
    
    public boolean getBoolean(String paramName, TemplateModel paramValue) throws TemplateModelException {
        if(paramValue == null) {
            return false;
        } else {
            if(paramValue instanceof TemplateScalarModel) {
                String value = ((TemplateScalarModel) paramValue).getAsString();
                return Boolean.valueOf(value);
            } else if(paramValue instanceof TemplateBooleanModel) {
                return ((TemplateBooleanModel) paramValue).getAsBoolean();
            } else {
                throw new TemplateModelException("boolean转换异常!");
            }
        }
    }
}